#ifndef MAINWINDOW_H
#define MAINWINDOW_H

enum RK_Project{
    PROJECT_3C = 1,
    PROJECT_312E0,
    PROJECT_31313,
    PROJECT_31323,
    PROJECT_38F1,
    PROJECT_38F2
};
#define VERSION                  3.9
#define PROJECT                  PROJECT_312E0

#define UI_INCLUDE_DIO           0
#define IS_WINDOWS               1
#define UI_INCLUDE_POWER_ADC     0

#define STATUS_LED_COUNT         36
#define UART_REVICE_BUF_SIZE     512
#define UART_SEND_BUF_SIZE       256
#define UART_DATA_HEAD_SIZE      8
#define MAX_RECV_BUF_SIZE   (10 * 1024)

#include <QMainWindow>
#include <QSerialPort>
#include <QTimer>
#include <qdatetime.h>
#include <QSerialPortInfo>
#include <qdebug.h>
#include <QProcess>

#include <stdio.h>
#include <stdlib.h>
#if IS_WINDOWS
#include <windows.h>
#include "../CH367DRV/LIB/CH367DLL.H"

#else
#include "ch36x_lib_linux.h"
#endif

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE


typedef struct _cycle_buf {
    unsigned int    head;
    unsigned int    tail;
    unsigned char   data_buf[MAX_RECV_BUF_SIZE];
}CYCLE_BUF;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    Ui::MainWindow *ui;

    void uart_data_handle();
private slots:

    void on_timer();

    void on_btn_open_clicked();

    void on_serversimuart_BtnRefresh_clicked();

    void on_uart_data_recive();

    void on_btn_save_clicked();

    void on_btn_reset_cpu1_2_clicked();

    void on_button_cpu1_12v_clicked();

    void on_button_cpu1_12v_2_clicked();

    void on_button_cpu1_12v_3_clicked();

    void on_button_cpu1_12v_4_clicked();

    void on_button_cpu1_12v_5_clicked();

    void on_button_cpu1_12v_6_clicked();

    void on_button_cpu1_12v_7_clicked();

    void on_button_cpu1_12v_8_clicked();

    void on_button_cpu1_12v_9_clicked();

    void on_button_cpu1_12v_10_clicked();

    void on_button_cpu1_12v_11_clicked();

    void on_button_cpu1_12v_12_clicked();

    void on_button_cpu1_12v_13_clicked();

    void on_button_cpu1_12v_14_clicked();

    void on_button_cpu1_12v_15_clicked();

    void on_button_cpu1_12v_16_clicked();

    void on_button_cpu1_12v_17_clicked();

    void on_button_cpu1_12v_18_clicked();

    void on_button_cpu1_12v_19_clicked();

    void on_button_cpu1_12v_20_clicked();

    void on_button_cpu1_12v_21_clicked();

    void on_button_cpu1_12v_22_clicked();

    void on_button_cpu1_12v_23_clicked();

    void on_btn_reset_cpu1_clicked();

    void on_btn_reset_cpu2_clicked();

    void on_btn_reset_cpu3_clicked();

    void on_btn_send_heart_clicked();

    void on_btn_send_param_clicked();

    void on_pushButton_clicked();

    void on_comboBox_2_currentIndexChanged(int index);

#if UI_INCLUDE_DIO
    void dio_init();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_5_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_6_clicked();

    void on_pushButton_10_clicked();

    void on_pushButton_11_clicked();

    void on_pushButton_12_clicked();

    void on_pushButton_7_clicked();

    void on_pushButton_8_clicked();

    void on_pushButton_9_clicked();

    void on_pushButton_13_clicked();

    void on_pushButton_14_clicked();

    void on_pushButton_15_clicked();
#endif

    void on_button_power_en_cpu_1_clicked();

    void on_button_power_en_cpu_2_clicked();

    void on_button_power_en_cpu_3_clicked();

    void on_pushButton_16_clicked();

    void on_btn_reset_cpu1_3_clicked();

    void on_btn_reset_cpu1_4_clicked();


    void on_btn_stop_clicked();

    void on_btn_continue_clicked();


private:
    void send_data_init();

    void float_input_restrict();
    void input_restrict();
    void status_led_init();
    void status_call();

    void uart_data_edit_show( unsigned char * data, uint len );
    void uart_data_buf_proc(unsigned char *data, unsigned short len);
    void cpu_power_value_get(unsigned char *data, unsigned char len);
    void cpu_power_threshold_get(unsigned char *data, unsigned char len);
    void cpu_power_status_get(unsigned char *data, unsigned char len);
    void fan_power_status_get(unsigned char *data, unsigned char len);
    void fan_IO_input_get(unsigned char *data, unsigned char len);
    void temperture_get(unsigned char *data, unsigned char len);
    void power_adc_value_get(unsigned char *data, unsigned char len);

    //heart
    unsigned int send_heart_cnt;
    unsigned int heart_timer;

    //flag
    bool flag_uart_open    = false;
    bool flag_pcie_open    = false;
    bool flag_DIO_change   = false;
    bool flag_uart_recive  = false;
    bool flag_do1_mode_pwm = false;
    bool flag_do2_mode_pwm = false;
    bool flag_do3_mode_pwm = false;
    bool flag_di_cnt_reset = true;

    //ch368
    int fd;                     /*文件描述符*/
    uint mIndex = 0;            /* 设备序号 */
    mPCH368_MEM_REG membaseaddr;/*Mem基寄存器*/
    unsigned char pcieio_timer;

    //serial
    QSerialPort     mSerial;
    QTimer          mTimer;
    CYCLE_BUF       mCycle_buf;
    unsigned char   zero_data_count;
    unsigned char   uart_recv_buf[UART_REVICE_BUF_SIZE];
    unsigned char   sendbuf[UART_SEND_BUF_SIZE];

    unsigned int  self_id;
    unsigned char new_old_abnormal_flag[STATUS_LED_COUNT] = {0};
    unsigned char new_old_abnormal_flag_call[STATUS_LED_COUNT] = {0};
    unsigned char new_old_abnormal_flag_last[STATUS_LED_COUNT] = {0};

    void read_pcie_io_data();
};

#endif // MAINWINDOW_H
